<template>
  <Panel>
    <template #title>个人中心</template>
    <template>
      <ul>
        <li>
          <span>管理员ID：</span>
          <p>{{ users.id }}</p>
        </li>
        <li>
          <span>账号：</span>
          <p>{{ users.account }}</p>
        </li>
        <li>
          <span>用户组：</span>
          <p>{{ users.userGroup }}</p>
        </li>
        <li>
          <span>创建时间：</span>
          <p>{{ users.ctime }}</p>
        </li>
        <li>
          <span>管理员头像</span>
          <el-upload
            class="avatar-uploader"
            action="http://localhost:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li class="save-img">
          <el-button type="primary" size="mini" @click="saveAvatar"
            >保存头像</el-button
          >
        </li>
      </ul>
    </template>
  </Panel>
</template>

<script>
import local from '@/utils/local'
import { uploadAvatar } from '@/api/user'

export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      imgStr: ''
    }
  },
  created() {
    this.users = JSON.parse(local.get('users'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // 把上传成功之后的图片地址存起来
      // this.$set(this, 'imgStr', res.imgUrl)
      this.imgStr = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
      // const isJPG = file.type === 'image/jpeg'
      // includes 数组的方法，判断当前的值是否包含在数组中
      const isBool = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isBool) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isBool && isLt2M
    },
    async saveAvatar() {
      const { code } = await uploadAvatar({
        imgUrl: this.imgStr
      })
      // console.log(data)
      if (code === 0) {
        location.reload()
      }
    }
  }
}
</script>

<style lang="less" scoped>
li {
  display: flex;
  line-height: 26px;
  margin: 10px 0;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  &:nth-last-of-type(2) {
    border: none;
  }

  span {
    flex: 0 0 100px;
    margin-right: 15px;
  }

  .el-avatar {
    width: 100px;
    height: 100px;
  }
}
.save-img {
  border: none;
  padding-left: 135px;
}
// less 的穿刺写法
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
